/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/core/PGLPolygonMarker.h>
#include <pgl/core/DataRegistry.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

PGL_IMPLEMENT_DYNCREATE(CPGLPolygonMarker,CPGLMarker);

#ifdef _DEBUG
void CPGLPolygonMarker::Dump( CDumpContext& dc ) const
{
    // call base class function first
    CPGLMarker::Dump( dc );

    // now do the stuff for our specific class
	dc << _T("--- CPGLPolygonMarker") << GetID() <<_T(" ---\n");
}

void CPGLPolygonMarker::AssertValid() const
{
    // call inherited AssertValid first
    CPGLMarker::AssertValid();
} 
#endif

void CPGLPolygonMarker::SerializeXML(CPGLArchive& ar)
{
	static const TCHAR szTag[] = _T("CPGLPolygonMarker");
	CString strPos;	strPos.Format(_T("polygonMarkerPos%u"),(DWORD)this);

	markup::CMarkupArchive& xml = *((markup::CMarkupArchive*)ar.GetMarkup());
	if(ar.IsStoring())
	{
		xml.AddChildElem(szTag);
		xml.AddChildAttrib(_T("Name"),GetName());
		xml.IntoElem();

			/////////////////////////////////////////////////////////
			// Adding base class
			CPGLMarker::SerializeXML(ar);

			xml.AddChildElemEx(_T("Closed"), m_bClosed);
			xml.AddChildElemEx(_T("Shape"));
			xml.IntoElem();
				if (m_pCornerX)
				{
					xml.AddChildElemEx(_T("X"));
					xml.IntoElem();
						m_pCornerX->SerializeXML(ar);
					xml.OutOfElem();
				}
				if (m_pCornerY)
				{
					xml.AddChildElemEx(_T("Y"));
					xml.IntoElem();
						m_pCornerY->SerializeXML(ar);
					xml.OutOfElem();
				}
			xml.OutOfElem();

		xml.OutOfElem();
	}
	else
	{
		if  (!xml.FindChildElem(szTag))
				return;

		ClearData();

		xml.IntoElem();

			/////////////////////////////////////////////////////////
			// loading base class
			CPGLMarker::SerializeXML(ar);

			xml.FindGetChildData(_T("Closed"), m_bClosed);

			xml.ResetChildPos();
			if (xml.FindChildElem(_T("Shape")))
			{
				xml.IntoElem();
					PGL_SERIALIZE_LOAD_DATA( strPos , X , m_pCornerX );
					PGL_SERIALIZE_LOAD_DATA( strPos , Y , m_pCornerY );
				xml.OutOfElem();
			}

		xml.OutOfElem();
	}
}


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPGLPolygonMarker::CPGLPolygonMarker(const CPGLColor& _color, const CPGLColor& _FillColor)
: CPGLMarker(_color, _FillColor), m_bClosed(true)
{
}

CPGLPolygonMarker& CPGLPolygonMarker::operator = (const CPGLPolygonMarker& p)
{
	if (this != &p)
	{
		this->CPGLMarker::operator =( p);

		m_bClosed = p.m_bClosed;

		ClearData();

		if (p.m_pCornerX)
			m_pCornerX=p.m_pCornerX->Clone();
		if (p.m_pCornerY)
			m_pCornerY=p.m_pCornerY->Clone();
	}

	return *this;
}

CPGLPolygonMarker::~CPGLPolygonMarker()
{
	ClearData();
}


void CPGLPolygonMarker::PlotGfx(gfxinterface::CGfxInterface& gfx, CPGLDataPtr pDataX, CPGLDataPtr pDataY)
{
	if (!pDataX || !pDataY || !IsVisible())
		return;

	const UINT n = __min( m_pCornerX->size(), m_pCornerY->size());

	if (!m_pCornerX || !m_pCornerY 
		|| n <3 
		|| pDataX->size() == 0 || pDataY->size() ==0)
		return;

	UpdateCorners(GetRadius());

	// drawing....
	CPGLMarker::PlotGfx(gfx, pDataX, pDataY);

	gfx.DrawPolygonStrip( *pDataX, *pDataY, m_vCornerX, m_vCornerY, GetLineType());
}

void CPGLPolygonMarker::PlotLegendGfx(gfxinterface::CGfxInterface& gfx, float x, float y)
{
	if (!IsVisible())
		return;

	if (!m_pCornerX || !m_pCornerY || m_pCornerX->size() <3 || m_pCornerY->size() < 3)
		return;

	UpdateCorners(GetLegendRadius());

	CPGLMarker::PlotLegendGfx(gfx, x,y);

	gfx.DrawPolygon( x, y, m_vCornerX, m_vCornerY, GetLineType());
}

void CPGLPolygonMarker::ClearData()
{
	m_pCornerX.reset();
	m_pCornerY.reset();
}

void CPGLPolygonMarker::SetDatas( CPGLDataPtr pXs, CPGLDataPtr pYs)
{
	ClearData();

	m_pCornerX = pXs;
	m_pCornerY = pYs;
}

void CPGLPolygonMarker::UpdateCorners(double radius)
{
	if (!m_pCornerX || !m_pCornerY)
	{
		m_vCornerX.resize(0);
		m_vCornerY.resize(0);
		return;
	}

	const UINT n = __min( m_pCornerX->size(), m_pCornerY->size());
	m_vCornerX.resize(n);
	m_vCornerY.resize(n);

	if (n==0)
		return;

	for (UINT i=0;i<n;i++)
	{
		m_vCornerX[i]=(float)((*m_pCornerX)[i]*radius);
		m_vCornerY[i]=(float)((*m_pCornerY)[i]*radius);
	}
}

DWORD CPGLPolygonMarker::GetLineType() const
{
	using namespace gfxinterface;
	
	DWORD eLineType = CPGLMarker::GetLineType();

	if (m_bClosed)
		eLineType |= LineTypeClosed;

	return eLineType;
}
